 class ListNode {
     int val;
     ListNode next;
     ListNode(int x) {
         val = x;
         next = null;
     }
 }

public class LinkedListCycle {
    public boolean hasCycle(ListNode head) {
        if(head == null) return false;

        ListNode p1 = head;        
        ListNode p2 = head;

        while(p1 != null && p2 != null && p2.next != null){
            p2 = p2.next;
            if(p2 == p1)
                return true;
            p2 = p2.next;
            if(p2 == p1)
                return true;
            p1 = p1.next;
        }
        return false;
    }
    public static void main(String[] args) {
        
    }
}